import os
import random
import time


from selenium.webdriver import ActionChains
from selenium.webdriver.support.select import Select

from Base.PublicFunction import readTable
from WoniuBossPOM.TeachingManagement_POM import TeachingManagement
from WoniuBossPOM.All_Management_POM import All_Mamagement

class TeachingManagementTest():
    # BASE_DIR = os.path.dirname(os.path.dirname(__file__))
    # TestDatadir = os.path.join(BASE_DIR,'TestData\\')
    def __init__(self):
        self.tm = All_Mamagement()

    # def login(self):
    #     self.tm.get_username_ele().send_keys('WNCD000')
    #     self.tm.get_password_ele().send_keys('woniu123')
    #     # self.tm.get_verifycode_ele().send_keys('0000')
    #     self.tm.get_login_button_ele().click()
    #     time.sleep(2)




    def add_apply(self,flag):
        if flag == 1:
            self.tm.get_teaching_management_ele().click()  # 点击教学管理

        time.sleep(2)
        self.tm.get_overtime_apply_ele().click()               # 点击加班申请
        table1 = readTable(self.tm.get_apply_table_ele())
        id = table1[0][0]
        # print(type(id))     str
        self.tm.get_add_button_ele().click()                   # 点击新增按钮

        self.tm.get_apply_start_time().click()                 # 点击加班申请开始时间
        self.tm.get_start_day_ele().click()                    # 点击日期
        time.sleep(2)
        self.tm.get_start_hour_ele().click()                   # 点击小时
        time.sleep(2)
        self.tm.get_start_minute_ele().click()                 # 点击分钟

        self.tm.get_apply_end_time().click()                   # 点击加班申请结束时间
        self.tm.get_end_day_ele().click()
        time.sleep(2)
        self.tm.get_end_hour_ele().click()
        time.sleep(2)
        self.tm.get_end_minute_ele().click()


        selectList = Select(self.tm.get_apply_accounting_way_ele())
        count = len(selectList.options)
        index = random.randint(1,count-1)
        selectList.select_by_index(index)           # 下拉框选择申请调休


        selectList = Select(self.tm.get_belong_region())
        count = len(selectList.options)
        index = random.randint(1,count-1)
        selectList.select_by_index(index)
        selectText = selectList.first_selected_option.text


        self.tm.get_apply_reason_ele().send_keys('aaaaaaaaaa')    # 输入申请原因

        self.tm.get_submit_button_ele().click()                   # 点击提交


        self.tm.get_confirm_button().click()                      # 点击确认
        # ActionChains(self.tm.driver).click(self.tm.get_confirm_button()).perform()
        # print(readTable(self.tm.get_apply_table_ele()))
        table2 = readTable(self.tm.get_apply_table_ele())
        # print(table2[0][0])
        # print(type(table2[0][0]))      str
        return id,table2,selectText

    def apply_reason(self):
        # self.tm.get_teaching_management_ele().click()  # 点击教学管理
        self.tm.get_overtime_apply_ele().click()  # 点击加班申请
        self.tm.get_add_button_ele().click()  # 点击新增按钮

        self.tm.get_apply_start_time().click()  # 点击加班申请开始时间
        self.tm.get_start_day_ele().click()  # 点击日期
        time.sleep(2)
        self.tm.get_start_hour_ele().click()  # 点击小时
        time.sleep(2)
        self.tm.get_start_minute_ele().click()  # 点击分钟

        self.tm.get_apply_end_time().click()  # 点击加班申请结束时间
        self.tm.get_end_day_ele().click()
        time.sleep(2)
        self.tm.get_end_hour_ele().click()
        time.sleep(2)
        self.tm.get_end_minute_ele().click()

        selectList = Select(self.tm.get_apply_accounting_way_ele())
        count = len(selectList.options)
        index = random.randint(1,count-1)
        selectList.select_by_index(index)
        accountText = selectList.first_selected_option.text

        self.tm.get_apply_reason_ele().send_keys('aaaaaaaaa')  # 输入申请原因

        self.tm.get_submit_button_ele().click()                   # 点击提交

        # print(self.tm.get_apply_reason_clue())
        time.sleep(2)        # 必须加延时，不然报错

        try:
            apply_reason_clue = self.tm.get_apply_reason_clue().text
            self.tm.get_exit_reason_clue().click()
            time.sleep(2)  # 必加
            self.tm.get_exit_apply().click()
            return apply_reason_clue
        except Exception as e:
            return '测试失败'





    def assign_work(self):
        # self.tm.get_teaching_management_ele().click()  # 点击教学管理
        self.tm.get_on_duty_ele().click()  # 点击教师值班
        self.tm.get_assign_work_ele().click()  # 点击指定值班
        time.sleep(2)
        selectList = Select(self.tm.get_workman_select_ele())
        count = len(selectList.options)  # 下拉框的长度
        index = random.randint(1, count - 1)   # 下标为0是全部，必须从1开始
        selectList.select_by_index(index)
        # print(self.tm.get_workman_select_ele().text)
        selectedText = selectList.first_selected_option.text

        # select_province_type = Select(self.driver.find_element_by_xpath('//*[@name="top"]'))  # 定位下拉框
        # print(select_province_type.first_selected_option.text)  # 获取下拉框当前选中得值
        # select_province_type.select_by_value('B')  # 选择下拉框得另一个选项
        # time.sleep(1)
        # print(select_province_type.first_selected_option.text)  # 重新获取下拉框选择的值



        self.tm.get_workdata_ele().click()  # 点击值班日期输入框
        # time.sleep(3)
        self.tm.get_date_ele().click()  # 点击日期
        # time.sleep(3)
        self.tm.get_dutyinfo_save_ele().click()  # 点击保存值班信息按钮
        self.tm.get_dutyinfo_confirm_ele().click()  # 点击确认按钮
        # print(readTable(self.tm.get_dutyinfo_table_ele()))
        return readTable(self.tm.get_dutyinfo_table_ele()),selectedText



    def sys_employee_query(self):
        self.tm.get_sys_management_ele().click()
        self.tm.get_user_management().click()
        self.tm.get_employee_name_ele().send_keys('尚天')
        self.tm.get_query_button_ele().click()
        return readTable(self.tm.get_sys_emloyee_query_table_ele())



    def close_browser(self):
        self.tm.closeBrowser()

if __name__ == '__main__':
    test = TeachingManagementTest()
    # test.login()
    # test.add_apply('申请调休','上海',1)
    # test.assign_work()